{% extends "base.html" %}
{% load i18n static bootstrap3 %}

{% block title %}{% trans "Submit Application" %}{% endblock %}
{% block extra_css %}
<link rel="stylesheet" type="text/css" href="{% static 'css/sciapplications.css' %}">
{% endblock %}

{% block content %}
<div class="row">
    <div class="col-md-12">
        <ol class="breadcrumb">
          <li><a href="{% url 'sciapplications:index' %}">Calls for Proposals</a></li>
          <li class="active">Submit proposal</li>
        </ol>
    </div>
</div>
<div class="row">
    <div class="col-md-9">
        <p>You are creating a proposal for {{ call.semester }}.</p>
        {% if call.proposal_type != 'DDT' %}
        <p>Deadline: <strong>{{ call.deadline|date:"j M Y P" }}</strong> ({{ call.deadline|timeuntil }})</p>
        {% endif %}
        <p>{{ call.eligibility }}</p>
        {% bootstrap_form_errors form %}
        <form method="post" action="" enctype="multipart/form-data" id="app_form">
            {% csrf_token %}

            {{ form.status }}
            {{ form.call }}

            {% if form.title %}
                {% bootstrap_field form.title %}
            {% endif %}

            {% if form.abstract %}
                {% bootstrap_field form.abstract %}
            {% endif %}

            {% if form.pi %}
                {% bootstrap_label 'Principal Investigator' label_for=id_pi %}
                <div class="help-block">
                {% blocktrans %}<strong>If this is not you</strong>, please add the email address of the PI.{% endblocktrans %}
                </div>
                {% bootstrap_field form.pi show_label=false %}
            {% endif %}

            {% if form.coi %}
                {% bootstrap_label 'Co-investigators' label_for=id_coi %}
                <div class="help-block">
                {% blocktrans %}
                Add the email addresses (comma separated) of co-investigators on this proposal.
                {% endblocktrans %}
                </div>
                {% bootstrap_field form.coi show_label=false %}
            {% endif %}

            {% if form.budget_details %}
                {% bootstrap_label 'Observing Budget' %}
                <div class="help-block">
                {% blocktrans %}Requested observing time in hours for the semester. Time for <a href="https://lco.global/documentation/rapid-response-mode/" target="_blank">Rapid Response mode</a>
                (within 15 minutes of submission) observations must be requested separately.
                {% endblocktrans %}
                </div>
                {% bootstrap_formset_errors timerequest_form %}
                <table class="table table-striped">
                    <thead><tr><td>Telescope Class</td><td>Standard Time</td><td>Rapid Response</td></tr></thead>
                    <tbody>
                    {% for form in timerequest_form.forms %}
                    {% bootstrap_form_errors form %}
                    {{ form.id }}
                    <tr class="timerequest-row">
                        <td>
                        {% if form.instance.pk %}{{ form.DELETE }}{% endif %}
                        {{ form.telescope_class }}
                        </td>
                        <td>{{ form.std_time }}</td>
                        <td>{{ form.too_time }}</td>
                    </tr>
                    {% endfor %}
                    </tbody>
                    {{ timerequest_form.management_form }}
                </table>

                {% bootstrap_label 'Observing Budget Explanation' label_for=id_budget_details %}
                <div class="help-block">
                Describe your observing strategy and justify the number of hours you are requesting. Requests for Rapid Response observations must be justified separately. If your targets are known, list them and their expected brightnesses.
                </div>
                {% bootstrap_field form.budget_details show_label=false %}
            {% endif %}

            {% if form.science_case %}
                {% bootstrap_label 'Science Justification' label_for=id_science_case %}
                <div class="help-block">
                {% blocktrans %}
                <p>
                Please provide scientific justification for the observations requested, including
                appropriate background information and the goals of the project. The results of any
                previous time allocated for this project should be discussed. Pertinent references
                should be listed in full.
                </p>

                <p>
                You may use the text field below, or you may upload a single file with a maximum of
                1 page of justification and 2 pages containing figures. We can only accept PDF files.
                </p>
                {% endblocktrans %}
                </div>
                {% bootstrap_field form.science_case show_label=False %}
                {% bootstrap_field form.science_case_file show_label=False %}
            {% endif %}

            {% if form.experimental_design %}
                {% bootstrap_label 'Experimental Design' label_for=id_experimental_design %}
                <div class="help-block">
                {% blocktrans %}
                <p>Describe the strategy of your observing program. Describe the characteristics of your
                targets, and justify the instruments and exposure times. Are there any unusual aspects of
                scheduling that might impact your success?</p>

                <p>If you are requesting Rapid Response observations, you must justify the need for the amount of time.
                (Because Rapid Response mode interrupts scheduled observations, its use is restricted to targets
                that can ONLY be observed in this way.)</p>

                <p>What measurements will you make from the data, and what additional work will you do to address your science goals?</p>

                <p>You may also upload a pdf if your experimental design is complex.</p>
                {% endblocktrans %}
                </div>
                {% bootstrap_field form.experimental_design show_label=False %}
                {% bootstrap_field form.experimental_design_file show_label=False %}
            {% endif %}

            {% if form.instruments %}
                {% bootstrap_label 'Instruments' label_for=id_instruments %}
                <div class="help-block">
                {% blocktrans %}
                The 2m telescopes have Spectral imagers and FLOYDS spectrographs.
                The 1m telescopes have Sinistro imagers; some have NRES spectrographs.
                The 0m4 telescopes have SBIG STX-6303 imagers. Ctrl+click to select multiple instruments.
                {% endblocktrans %}
                </div>
                {% bootstrap_field form.instruments show_label=False %}
            {% endif %}

            {% if form.moon %}
                {% bootstrap_field form.moon %}
            {% endif %}

            {% if form.related_programs %}
                {% bootstrap_label 'Related programs on other telescopes' label_for=id_related_programs %}
                {% bootstrap_field form.related_programs show_label=False %}
            {% endif %}

            {% if form.past_use %}
                {% bootstrap_label 'Report on past use of LCO in the last 3 years' label_for=id_past_use %}
                {% bootstrap_field form.past_use show_label=False %}
            {% endif %}

            {% if form.publications %}
                {% bootstrap_label 'Applicant&#39s related publications'  %}
                <div class="help-block">
                {% blocktrans %}
                Up to 10 relevant publications from the past 3 years.
                {% endblocktrans %}
                </div>
                {% bootstrap_field form.publications show_label=False %}
            {% endif %}

            {% if form.management %}
                {% bootstrap_label 'Plan for management of entire scientific investigation' label_for=id_management %}
                {% bootstrap_field form.management show_label=False %}
            {% endif %}

            {% if form.relevance %}
                {% bootstrap_label 'Relevance to unique capabilities of the LCO network' label_for=id_relevance %}
                {% bootstrap_field form.relevance show_label=False %}
            {% endif %}

            {% if form.contribution %}
                {% bootstrap_label 'Contribution brought by non-LCO participants' label_for=id_contribution %}
                {% bootstrap_field form.contribution show_label=False %}
            {% endif %}

            {% if form.science_justification %}
                {% bootstrap_field form.science_justification %}
            {% endif %}

            {% if form.ddt_justification %}
                {% bootstrap_label 'Justification of discretionary time' label_for=id_ddt_justification %}
                <div class="help-block">
                Discretionary time is reserved for observations of unforeseen targets-of-opportunity. Describe why this proposal must be reviewed now, rather than as part of the normal time allocation process for the upcoming semester.
                </div>
                {% bootstrap_field form.ddt_justification show_label=False placeholder='DDT Justification' %}
            {% endif %}
        </form>
        <a href="#">Back to top</a>
    </div>
    <div class="col-md-3">
        <h3>{% trans 'Application status' %}:
            {% if form.instance.id %}
                {{ form.status.value }}
            {% else %}
                {% trans 'UNSAVED' %}
            {% endif %}
        </h3>
        {% blocktrans %}
        <p>You may save your application and work on it later.</p>
        <p>Once you submit your application, you will no longer be able to edit it.</p>
        {% endblocktrans %}
        {% buttons %}
        <a href="#" class="btn btn-default" id="save">{% trans 'Save' %}</a>
        <a href="#" class="btn btn-success" id="submit">{% trans 'Submit' %}</a>
        {% endbuttons %}
        {% if form.instance.id %}
        <p>Previewing your submission will <strong>discard</strong> any pending edits.</p>
        <a href="{% url 'sciapplications:detail' form.instance.id %}" class="btn btn-default" id="preview">{% trans 'Preview' %}</a>
        <p>{% trans 'Deleting a draft is an irreversible action' %}.</p>
        <p><a href="{% url 'sciapplications:delete' pk=form.instance.id %}" class="btn btn-danger">{% trans 'Delete' %}</a></p>
        {% endif %}
    </div>
</div>
{% endblock %}
{% block extra_javascript %}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.formset/1.2.2/jquery.formset.min.js"></script>
<script type="text/javascript">
    function check_time_requests(){
         if($('.timerequest-row').length < 2){
            $('.fa-trash').hide();
        }else{
            $('.fa-trash').show();
        }
    }
    $(document).ready(function(){
        $('#app_form tbody tr').formset({
            prefix: '{{ timerequest_form.prefix }}',
            deleteCssClass: 'delete-timeallocation',
            deleteText: '<i class="fa fa-trash"></i>',
            added: function(row){
               row.find('input[type=number]').val(0);
               check_time_requests();
            },
            removed: function(row){
                check_time_requests();
            }
        });
        check_time_requests();
    });
    $('#submit').click(function(){
        if(window.confirm('Submit this application? You will no longer be able to edit it')){
            $('#id_status').val('SUBMITTED')
            $('#app_form').submit()
        }
    });
    $('#save').click(function(){
        $('#id_status').val('DRAFT')
        $('#app_form').submit()
    });
    $("#id_science_case_file").change(function(){
        if(!$("#id_science_case").val()){
            $("#id_science_case").val('Attached')
        }
    });
    $("#id_experimental_design_file").change(function(){
        if(!$("#id_experimental_design").val()){
            $("#id_experimental_design").val('Attached')
        }
    });
</script>
{% endblock %}
